﻿using System.Drawing;
using System.Drawing.Drawing2D;

namespace SmartMotion.Core
{
    public class ResizeImageHelper
    {
        public static Image ResizeImage(Image imgToResize, Size size)
        {
            //获取图片宽度
            int sourceWidth = imgToResize.Width;
            //获取图片高度
            int sourceHeight = imgToResize.Height;
            //计算宽度的缩放比例
            float nPercentW = size.Width / (float)sourceWidth;
            //计算高度的缩放比例
            float nPercentH = size.Height / (float)sourceHeight;
            var nPercent = nPercentH < nPercentW ? nPercentH : nPercentW;
            //期望的宽度
            int destWidth = (int)(sourceWidth * nPercent);
            //期望的高度
            int destHeight = (int)(sourceHeight * nPercent);
            var b = new Bitmap(destWidth, destHeight);
            var g = Graphics.FromImage(b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //绘制图像
            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();
            return b;
        }

    }
}